/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
/**
 * @description GLCodeCombinationsServiceImpl.java
 * @author dongbin
 * @version 
 * @copyright
 */

package com.erp.common.account.service.spring;

import org.apache.commons.lang.StringUtils;
import org.redragon.commons.lang.JavaReflect;
import org.redragon.framework.util.RedisUtil;
import org.redragon.framework.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.common.account.dao.GLCodeCombinationsDao;
import com.erp.common.account.dao.GLLedgerDao;
import com.erp.common.account.dao.model.GLCodeCombinations;
import com.erp.common.account.dao.model.GLLedger;
import com.erp.common.account.param.GLParam;
import com.erp.common.account.service.GLCodeCombinationsService;
import com.erp.common.account.service.GLLedgerService;
import com.erp.masterdata.common.service.MasterDataCommonService;

/**
 * @description
 * @date 2021年2月5日
 * @author dongbin
 * 
 */
@Service("glCodeCombinationsServiceCommon")
@Transactional(rollbackFor=Exception.class)
public class GLCodeCombinationsServiceImpl implements GLCodeCombinationsService{
    
    //注入Dao
    @Autowired
    @Qualifier("glCodeCombinationsDaoCommon")
    private GLCodeCombinationsDao glCodeCombinationsDao;
    @Autowired
    @Qualifier("glLedgerServiceCommon")
    private GLLedgerService glLedgerService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    
    @Override
    public String getGLCodeCombinationsSegment(String codeCombinationCode) {
        String segment = "";
        
        //获取redis缓存
        String segmentCache = RedisUtil.getValue(GLParam.GL_SUBJECT_SEGMENT_CACHE_KEY+codeCombinationCode);
        
        if(StringUtils.isNotBlank(segmentCache)) {
            segment = segmentCache;
        }else {
            //获取科目结构
            GLCodeCombinations glCodeCombinations = this.glCodeCombinationsDao.getDataObject(codeCombinationCode);
            
            if(glCodeCombinations!=null) {
                if(StringUtils.isNotBlank(glCodeCombinations.getSegment1())) {
                    segment = glCodeCombinations.getSegment1();
                }
                
                //反射获取segment值
                JavaReflect reflect = new JavaReflect(glCodeCombinations.getClass());
                reflect.setObject(glCodeCombinations);
                for(int a=2;a<=15;a++) {
                    String segmentTemp = (String)reflect.doMethod("getSegment"+a, null, null);
                    if(StringUtils.isNotBlank(segmentTemp)) {
                        segment = segment+"."+segmentTemp;
                    }else {
                        break;
                    }
                }
                
                //设置redis缓存
                if(StringUtils.isNotBlank(segment)) {
                    RedisUtil.setValue(GLParam.GL_SUBJECT_SEGMENT_CACHE_KEY+codeCombinationCode, segment, 864000);
                }
            }
            
        }
        
        return segment;
    }
    
    @Override
    public String getGLCodeCombinationsSegmentDesc(String codeCombinationCode) {
        String segmentDesc = "";
        
        //获取redis缓存
        String segmentDescCache = RedisUtil.getValue(GLParam.GL_SUBJECT_SEGMENT_DESC_CACHE_KEY+codeCombinationCode);
        
        if(StringUtils.isNotBlank(segmentDescCache)) {
            segmentDesc = segmentDescCache;
        }else {
            //获取科目结构
            GLCodeCombinations glCodeCombinations = this.glCodeCombinationsDao.getDataObject(codeCombinationCode);
            
            if(glCodeCombinations!=null) {
                //获取科目结构含义
                GLLedger glLedger = this.glLedgerService.getPrimaryGLLedgerByLegal(ShiroUtil.getLegalCode());
                
                
                if(StringUtils.isNotBlank(glCodeCombinations.getSegment1())) {
                    segmentDesc = this.getGLCodeCombinationsSingleSegmentDesc(glLedger.getSegment1(), glCodeCombinations.getSegment1());
                }
                
                //反射获取segment值
                JavaReflect reflectLedger = new JavaReflect(glLedger.getClass());
                reflectLedger.setObject(glLedger);
                
                JavaReflect reflect = new JavaReflect(glCodeCombinations.getClass());
                reflect.setObject(glCodeCombinations);
                
                for(int a=2;a<=15;a++) {
                    String segmentLedgerTemp = (String)reflectLedger.doMethod("getSegment"+a, null, null);
                    String segmentTemp = (String)reflect.doMethod("getSegment"+a, null, null);
                    
                    if(StringUtils.isNotBlank(segmentLedgerTemp)&&StringUtils.isNotBlank(segmentTemp)) {
                        segmentDesc = segmentDesc+"."+this.getGLCodeCombinationsSingleSegmentDesc(segmentLedgerTemp, segmentTemp);
                    }else {
                        break;
                    }
                }
                
                //设置redis缓存
                if(StringUtils.isNotBlank(segmentDesc)) {
                    RedisUtil.setValue(GLParam.GL_SUBJECT_SEGMENT_DESC_CACHE_KEY+codeCombinationCode, segmentDesc, 864000);
                }
            }
            
        }
        
        return segmentDesc;
    }
    
    @Override
    public String getGLCodeCombinationsSingleSegmentDesc(String segmentField, String segmentValue) {
        if(segmentField.contentEquals("SEGMENT_COMPANY")) {
            String value = this.masterDataCommonService.getCompanyMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_COST_CENTER")) {
            String value = this.masterDataCommonService.getCostCenterMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_SUBJECT")) {
            String value = this.masterDataCommonService.getSubjectMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_DETAIL_SUBJECT")) {
            String value = this.masterDataCommonService.getSubjectDetailMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_PROJECT")) {
            String value = this.masterDataCommonService.getProjectMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_PRODUCT")) {
            String value = this.masterDataCommonService.getMaterialMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_CUSTOMER")) {
            String value = this.masterDataCommonService.getCustomerMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_VENDOR")) {
            String value = this.masterDataCommonService.getVendorMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }else if(segmentField.contentEquals("SEGMENT_INTER_COMPANY")) {
            String value = this.masterDataCommonService.getCompanyMap().get(segmentValue);
            return StringUtils.isBlank(value)?segmentValue:value;
        }
        return null;
    }

}
